local joy__xiakexing = fk.CreateSkill {

  name = "joy__xiakexing",

  tags = {  },

}



joy__xiakexing:addEffect(fk.CardUsing, {
  name = "joy__xiakexing",
  events = {fk.CardUsing, fk.Damage},
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(joy__xiakexing.name) and target == player then
      if event == fk.CardUsing then
        return data.card.sub_type == Card.SubtypeWeapon and string.find(Fk:translate(data.card.name, "zh_CN"), "剑") ~= nil
      else
        return player:getEquipment(Card.SubtypeWeapon) ~= nil and not data.to.dead and player:canPindian(data.to)
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    return event == fk.CardUsing or player.room:askForSkillInvoke(player, joy__xiakexing.name, nil, "#joy__xiakexing-invoke:"..data.to.id)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.CardUsing then
      local card = Fk:cloneCard("archery_attack")
      card.skillName = joy__xiakexing.name
      if not player:prohibitUse(card) and player:canUse(card) then
        room:useCard{from = player.id, card = card, tos = {}}
      end
    else
      local pindian = player:pindian({data.to}, joy__xiakexing.name)
      if pindian.results[data.to.id].winner == player then
        room:changeMaxHp(data.to, -1)
      else
        local throw = player:getEquipments(Card.SubtypeWeapon)
        if #throw > 0 then
          room:throwCard(throw, joy__xiakexing.name, player, player)
        end
      end
    end
  end,
})
joy__xiakexing:addEffect(fk.Damage, {
  name = "joy__xiakexing",
  events = {fk.CardUsing, fk.Damage},
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(joy__xiakexing.name) and target == player then
      if event == fk.CardUsing then
        return data.card.sub_type == Card.SubtypeWeapon and string.find(Fk:translate(data.card.name, "zh_CN"), "剑") ~= nil
      else
        return player:getEquipment(Card.SubtypeWeapon) ~= nil and not data.to.dead and player:canPindian(data.to)
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    return event == fk.CardUsing or player.room:askForSkillInvoke(player, joy__xiakexing.name, nil, "#joy__xiakexing-invoke:"..data.to.id)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.CardUsing then
      local card = Fk:cloneCard("archery_attack")
      card.skillName = joy__xiakexing.name
      if not player:prohibitUse(card) and player:canUse(card) then
        room:useCard{from = player.id, card = card, tos = {}}
      end
    else
      local pindian = player:pindian({data.to}, joy__xiakexing.name)
      if pindian.results[data.to.id].winner == player then
        room:changeMaxHp(data.to, -1)
      else
        local throw = player:getEquipments(Card.SubtypeWeapon)
        if #throw > 0 then
          room:throwCard(throw, joy__xiakexing.name, player, player)
        end
      end
    end
  end,
})

return joy__xiakexing